﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SubtitleDownloader
{
	public partial class ProgramSettings : Form
	{
		private List<string> _tempWatchedSeriesFolders;
		private List<string> _tempWatchedMoviesFolders;
		private Dictionary<string, string> _tempTranslators;
		private List<string> _tempFileExtensions;
		private RegistryKey StartupKey { get { return Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); } }
		private bool StartOnStartup
		{
			get
			{
				var regValue = StartupKey.GetValue("SubtitleDownloader");
				if (regValue == null)
					return false;
				return bool.Parse((string)regValue);
			}
			set
			{
				StartupKey.SetValue("SubtitleDownloader", value);
			}
		}

		public bool IntervalChanged { get; private set; }

		public ProgramSettings()
		{
			InitializeComponent();
			tsBtnRemoveTranslator.Enabled = false;

			_tempTranslators = Data.Instance.CustomNameTranslator;
			foreach (var key in _tempTranslators.Keys)
				lbTranslators.Items.Add(key + " = " + _tempTranslators[key]);

			_tempFileExtensions = Data.Instance.FileExtensions;
			lbFileExtensions.Items.AddRange(_tempFileExtensions.ToArray());

			_tempWatchedSeriesFolders = Data.Instance.WatchedSeriesFolders;
			lbWatchedSeriesFolders.Items.AddRange(_tempWatchedSeriesFolders.ToArray());
			_tempWatchedMoviesFolders = Data.Instance.WatchedMoviesFolders;
			lbWatchedMoviesFolders.Items.AddRange(_tempWatchedMoviesFolders.ToArray());

			numUpDownSimConnections.Value = Data.Instance.MaxSimConnections;
			numUpDownScanInterval.Value = Data.Instance.UpdateInterval;
			chkBoxRunAtStartup.Checked = StartOnStartup;

			IntervalChanged = false;
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			StartOnStartup = chkBoxRunAtStartup.Checked;
			var updateInterval = (int)numUpDownScanInterval.Value;
			IntervalChanged = Data.Instance.UpdateInterval != updateInterval;
			Data.Instance.UpdateInterval = updateInterval;
			Data.Instance.MaxSimConnections = (int)numUpDownSimConnections.Value;
			Data.Instance.CustomNameTranslator = _tempTranslators;
			Data.Instance.FileExtensions = _tempFileExtensions;
			Data.Instance.WatchedSeriesFolders = _tempWatchedSeriesFolders;
			Data.Instance.WatchedMoviesFolders = _tempWatchedMoviesFolders;
			Data.Save();
			this.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.Close();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		}

		#region Translators

		private void lbTranslators_SelectedIndexChanged(object sender, EventArgs e)
		{
			tsBtnRemoveTranslator.Enabled = lbTranslators.SelectedIndex > -1;
		}

		private void tsBtnAddTranslator_Click(object sender, EventArgs e)
		{
			if (tsTxtTranslatorReplace.Text.Length == 0 || tsTxtTranslatorWith.Text.Length == 0)
				return;

			if (_tempTranslators.ContainsKey(tsTxtTranslatorReplace.Text))
				return;

			_tempTranslators.Add(tsTxtTranslatorReplace.Text, tsTxtTranslatorWith.Text);
			lbTranslators.Items.Add(tsTxtTranslatorReplace.Text + " = " + tsTxtTranslatorWith.Text);

			tsTxtTranslatorReplace.Clear();
			tsTxtTranslatorWith.Clear();
		}

		private void tsBtnRemoveTranslator_Click(object sender, EventArgs e)
		{
			var index = lbTranslators.SelectedIndex;
			if (index < 0)
				return;

			var split = ((string)lbTranslators.Items[index]).Split('=');
			var key = "";
			if (split.Length == 2)
				key = split[0].Trim();

			_tempTranslators.Remove(key);
			lbTranslators.Items.RemoveAt(lbTranslators.SelectedIndex);

			tsBtnRemoveTranslator.Enabled = false;
		}

		#endregion

		#region Extensions

		private void tsBtnAddFileExtension_Click(object sender, EventArgs e)
		{
			var txt = tstTxtFileExtension.Text;
			if (txt.Length > 0) {
				tstTxtFileExtension.Clear();
				var fixedText = txt.StartsWith(".") ? "" : "." + txt;
				lbFileExtensions.Items.Add(fixedText);
				_tempFileExtensions.Add(fixedText);
			}
		}

		private void tsBtnRemoveFileExtension_Click(object sender, EventArgs e)
		{
			var index = lbFileExtensions.SelectedIndex;
			if (index < 0)
				return;

			lbFileExtensions.Items.RemoveAt(index);
			_tempFileExtensions.RemoveAt(index);
		}

		#endregion

		#region Folders

		private void tsBtnAddSeriesFolder_Click(object sender, EventArgs e)
		{
			var folder = Utils.BrowseForFolder();
			if (folder != null) {
				if (!_tempWatchedSeriesFolders.Contains(folder)) {
					_tempWatchedSeriesFolders.Add(folder);
					lbWatchedSeriesFolders.Items.Add(folder);
				}
			}
		}

		private void tsBtnRemoveSeriesFolder_Click(object sender, EventArgs e)
		{
			var index = lbWatchedSeriesFolders.SelectedIndex;
			if (index < 0)
				return;

			lbWatchedSeriesFolders.Items.RemoveAt(index);
			_tempWatchedSeriesFolders.RemoveAt(index);
		}

		private void tsBtnAddMoviesFolder_Click(object sender, EventArgs e)
		{
			var folder = Utils.BrowseForFolder();
			if (folder != null) {
				if (!_tempWatchedMoviesFolders.Contains(folder)) {
					_tempWatchedMoviesFolders.Add(folder);
					lbWatchedMoviesFolders.Items.Add(folder);
				}
			}
		}

		private void tsBtnRemoveMovieFolder_Click(object sender, EventArgs e)
		{
			var index = lbWatchedMoviesFolders.SelectedIndex;
			if (index < 0)
				return;

			lbWatchedMoviesFolders.Items.RemoveAt(index);
			_tempWatchedMoviesFolders.RemoveAt(index);
		}

		#endregion
	}
}
